document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const taskInput = document.getElementById('taskInput');
    const addTaskButton = document.getElementById('addTask');
    const taskTableBody = document.querySelector('#taskTable tbody');
    const showAllButton = document.getElementById('showAll');
    const showWaitingButton = document.getElementById('showWaiting');
    const showDoingButton = document.getElementById('showDoing');

    // 添加任务
    addTaskButton.addEventListener('click', function() {
        const taskText = taskInput.value.trim();
        if (taskText) {
            const row = document.createElement('tr');
            const statusCell = document.createElement('td');
            statusCell.classList.add('status', 'waiting');
            statusCell.textContent = 'Waiting';
            const taskCell = document.createElement('td');
            taskCell.textContent = taskText;
            row.appendChild(statusCell);
            row.appendChild(taskCell);
            taskTableBody.appendChild(row);
            taskInput.value = ''; // 清空输入框

            // 绑定点击事件来切换状态
            row.addEventListener('click', function() {
                if (statusCell.classList.contains('waiting')) {
                    statusCell.classList.remove('waiting');
                    statusCell.classList.add('doing');
                    statusCell.textContent = 'Doing';
                }
            });
        }
    });

    // 过滤任务
    function filterTasks(filter) {
        const rows = taskTableBody.getElementsByTagName('tr');
        for (let i = 0; i < rows.length; i++) {
            const statusCell = rows[i].querySelector('.status');
            if (statusCell) {
                rows[i].style.display = filter === statusCell.textContent || filter === 'All' ? '' : 'none';
            }
        }
    }

    showAllButton.addEventListener('click', function() {
        filterTasks('All');
    });

    showWaitingButton.addEventListener('click', function() {
        filterTasks('Waiting');
    });

    showDoingButton.addEventListener('click', function() {
        filterTasks('Doing');
    });
});